package business;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;

import constant.Constant;
import dbAccess.ServicesImpl;
import dto.DTOAbsence;
import dto.DTOClass;
import dto.DTOCompany;
import dto.DTOContact;
import dto.DTORelation;

/**
 * @author Jonathan Maret
 * Implementation of the business interfaces
 */
public class BusinessImpl implements BusinessService {
	private ServicesImpl servicesImpl;

	public BusinessImpl() {
		servicesImpl = new ServicesImpl();
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllContact()
	 */
	@Override
	public ArrayList<DTOContact> getAllContact() {
		return servicesImpl.getAllContact();
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllStudent()
	 */
	@Override
	public ArrayList<DTOContact> getAllStudent() {
		ArrayList<DTOContact> ret = new ArrayList<DTOContact>();

		for (DTOContact dtoContact : getAllContact()) {
			if(dtoContact.getTypeOfContact() == Constant.typeofcontact("STUDENT"))
				ret.add(dtoContact);
		}	

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllStudentAvailable(dto.DTOClass)
	 */
	@Override
	public ArrayList<DTOContact> getAllStudentAvailable(DTOClass clasS) {
		ArrayList<DTOContact> ret = new ArrayList<DTOContact>();

		if(clasS != null){
			for (DTOContact dtoContact : getAllStudent()) {
				if(getRelation(dtoContact,clasS) == null)
					ret.add(dtoContact);				
			}
		}
		else
			ret = getAllStudent();

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllTeacher()
	 */
	@Override
	public ArrayList<DTOContact> getAllTeacher() {
		ArrayList<DTOContact> ret = new ArrayList<DTOContact>();

		for (DTOContact dtoContact : getAllContact()) {
			if(dtoContact.getTypeOfContact() == Constant.typeofcontact("TEACHER"))
				ret.add(dtoContact);
		}	

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllTeacherAvailable(dto.DTOClass)
	 */
	@Override
	public ArrayList<DTOContact> getAllTeacherAvailable(DTOClass clasS) {
		ArrayList<DTOContact> ret = new ArrayList<DTOContact>();

		if(clasS != null){
			for (DTOContact dtoContact : getAllTeacher()) {
				if(getRelation(dtoContact,clasS) == null){
					ret.add(dtoContact);
				}
			}
		}
		else
			ret = getAllTeacher();

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getContactByVisa(java.lang.String)
	 */
	@Override
	public DTOContact getContactByVisa(String visa) {
		return servicesImpl.getContactByVisa(visa);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getContactById(int)
	 */
	@Override
	public DTOContact getContactById(int id) {
		return servicesImpl.getContactById(id);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllClass()
	 */
	@Override
	public ArrayList<DTOClass> getAllClass() {
		return servicesImpl.getAllClass();
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getClassByName(java.lang.String)
	 */
	@Override
	public DTOClass getClassByName(String name) {
		return servicesImpl.getClassByName(name);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getClassByContact(dto.DTOContact)
	 */
	@Override
	public ArrayList<DTOClass> getClassByContact(DTOContact contact) {
		ArrayList<DTOClass> ret = new ArrayList<DTOClass>();

		for (DTORelation dtoRelation : servicesImpl.getAllRelationByContact(contact)) {
			boolean classIsNotPresent = true;
			for (DTOClass dtoClass : ret) {
				if(dtoClass.getId() == dtoRelation.getClass_id())
					classIsNotPresent = false;
			}

			if(classIsNotPresent)
				ret.add(servicesImpl.getClassById(dtoRelation.getClass_id()));
		}

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getRelation(dto.DTOContact, dto.DTOClass)
	 */
	@Override
	public DTORelation getRelation(DTOContact contact, DTOClass clasS) {
		return servicesImpl.getRelation(contact, clasS);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getContactsByClass(dto.DTOClass)
	 */
	@Override
	public ArrayList<DTOContact> getContactsByClass(DTOClass clasS) {
		ArrayList<DTOContact> ret = new ArrayList<DTOContact>();

		for (DTORelation dtoRelation : servicesImpl.getAllRelationByClass(clasS)) {
			boolean contactIsNotPresent = true;

			for (DTOContact dtoContact : ret) {
				if(dtoContact.getId() == dtoRelation.getContact_id())
					contactIsNotPresent = false;
			}

			if(contactIsNotPresent)
				ret.add(servicesImpl.getContactById(dtoRelation.getContact_id()));
		}

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAbsenceByContactByDate(dto.DTOContact, java.util.Calendar)
	 */
	@Override
	public DTOAbsence getAbsenceByContactByDate(DTOContact contact, Calendar calendar) {
		DTOAbsence ret = null;

		ArrayList<DTOAbsence> absence = servicesImpl.getAbsenceByContact(contact);

		for (DTOAbsence dtoAbsence : absence) {
			if(Constant.getZeroTimeDate(dtoAbsence.getDate()).compareTo(Constant.getZeroTimeDate(calendar.getTime())) == 0)
				ret = dtoAbsence;
		}

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#updateAbsence(dto.DTOClass, dto.DTOContact, java.util.Calendar, dto.DTOAbsence)
	 */
	@Override
	public void updateAbsence(DTOContact contact,Calendar calendar, DTOAbsence absence) {
		DTOAbsence generatedAbsence = new DTOAbsence(0, contact.getId(), calendar.getTime(), absence.getPeriod(),absence.getTypeOfAbsence(),absence.getComment(),absence.isMailSent());
		servicesImpl.updateAbsence(generatedAbsence);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllRelationByContact(dto.DTOContact)
	 */
	@Override
	public ArrayList<DTORelation> getAllRelationByContact(DTOContact contact) {
		return servicesImpl.getAllRelationByContact(contact);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllRelationByClass(dto.DTOClass)
	 */
	@Override
	public ArrayList<DTORelation> getAllRelationByClass(DTOClass clasS) {
		return servicesImpl.getAllRelationByClass(clasS);
	}


	/* (non-Javadoc)
	 * @see business.BusinessService#updateClass(dto.DTOClass, dto.DTOContact, java.util.ArrayList, java.util.ArrayList)
	 */
	@Override
	public void updateClass(DTOClass clasS, DTOContact holder,ArrayList<DTOContact> teacher, ArrayList<DTOContact> student) {
		servicesImpl.updateClass(clasS, holder, teacher, student);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#removeClass(dto.DTOClass)
	 */
	@Override
	public void removeClass(DTOClass clasS) {
		servicesImpl.removeClass(clasS);		
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#sortClass(java.util.ArrayList)
	 */
	@Override
	public ArrayList<DTOClass> sortClass(ArrayList<DTOClass> clasS) {
		Collections.sort(clasS, new Comparator<Object>() {
			@Override
			public int compare(Object e1, Object e2) {
				String id1 = ((DTOClass) e1).getName();
				String id2 = ((DTOClass) e2).getName();

				// ascending order
				return id1.compareTo(id2);

				// descending order
				// return id2.compareTo(id1);
			}
		});
		return clasS;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#sortContact(java.util.ArrayList)
	 */
	@Override
	public ArrayList<DTOContact> sortContact(ArrayList<DTOContact> contact) {
		if(contact != null)
			Collections.sort(contact, new Comparator<Object>() {
				@Override
				public int compare(Object e1, Object e2) {
					String id1 = ((DTOContact) e1).toString();
					String id2 = ((DTOContact) e2).toString();

					// ascending order
					return id1.compareTo(id2);

					// descending order
					// return id2.compareTo(id1);
				}
			});
		return contact;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#updateContact(dto.DTOContact, java.util.ArrayList)
	 */
	@Override
	public void updateContact(DTOContact contact,ArrayList<DTOClass> clasS) {
		servicesImpl.updateContact(contact,clasS);

	}

	/* (non-Javadoc)
	 * @see business.BusinessService#removeContact(dto.DTOContact)
	 */
	@Override
	public void removeContact(DTOContact contact) {
		servicesImpl.removeContact(contact);

	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllClassAvailable(dto.DTOContact)
	 */
	@Override
	public ArrayList<DTOClass> getAllClassAvailable(DTOContact contact) {
		ArrayList<DTOClass> ret = new ArrayList<DTOClass>();
		if(contact != null){
			for (DTOClass dtoClass : getAllClass()) {
				if(getRelation(contact, dtoClass) == null)
					ret.add(dtoClass);
			}
		}
		else
			ret = getAllClass();

		return ret;
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getClassById(int)
	 */
	@Override
	public DTOClass getClassById(int id) {
		return servicesImpl.getClassById(id);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAbsenceByContact(dto.DTOContact)
	 */
	@Override
	public ArrayList<DTOAbsence> getAbsenceByContact(DTOContact contact) {

		return servicesImpl.getAbsenceByContact(contact);
	}

	/* (non-Javadoc)
	 * @see business.BusinessService#getAllCompany()
	 */
	@Override
	public ArrayList<DTOCompany> getAllCompany() {
		return servicesImpl.getAllCompany();
	}

	@Override
	public ArrayList<DTOAbsence> getDailyAbsence(Calendar day) {
		return servicesImpl.getDailyAbsence(day);
	}





}
